<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\NodeTrait;

class Area extends Model
{
    use \App\Models\Traits\AreaQuery, NodeTrait;
    
    protected $table = 'sy_area';
    
    protected $primaryKey = 'AreaId';
    
    protected $fillable = ['Fid','AreaName','Co_ID','area_manager','connect_phone', 'area_text', 'lnglat', 'area_code', 'is_default', 'area_img', 'area_icon'];
    
    public $timestamps = false;
    
    protected $casts = [
        'lnglat' => 'array',
		'is_default' => 'boolean',
        'Co_ID' => 'integer',
        'AreaId' => 'string',
        'Fid' => 'string',
        'area_code' => 'string',
    ];
    
    //上级区域
    public function parentAreaName() {
        return $this->belongsTo('\App\Models\Area', 'Fid', 'AreaId');
    }
    
    //下级区域
    public function children2() {
        return $this->hasMany('\App\Models\Area', 'Fid', 'AreaId');
    }
    
    //公司
    public function company() {
        return $this->belongsTo('\App\Models\Company', 'Co_ID', 'Co_ID');
    }

    //设备
    public function devices() {
        return $this->hasMany('\App\Models\PriDeviceInfo', 'AreaId', 'AreaId');
    }
    
    //经纬度
    public function setLnglatAttribute($value)
    {
        if ($value) {
            $this->attributes['lnglat'] = json_encode($value);
        }
    }
    
    //筛选公司
    public function scopeCompany($query, $cid) {
        return $query->where('Co_ID', $cid);
    }
    
    //筛选字段
    public function scopeArea($query, $areaId) {
        if($areaId){
            return $query->whereIn('AreaId', $areaId);
        }
       return $query;
    }
    
   public function scopeChild($query, $areaId) {
        if($areaId){
            return $query->where('Fid', $areaId);
        }
       return $query;
    }
    
    public function getLftName()
    {
        return '_lft';
    }

    public function getRgtName()
    {
        return '_rgt';
    }

    public function getParentIdName()
    {
        return 'Fid';
    }
	
	/**protected function getScopeAttributes()
	{
		return [ 'Co_ID' ];
	}*/

    // Specify parent id attribute mutator
    public function setFidAttribute($value)
    {
        $this->setParentIdAttribute($value);
    }
    
    // public function makeRoot()
    // {
    //     $this->setParent(0)->dirtyBounds();

    //     return $this->setNodeAction('root');
    // }
    
    // protected function setParent($value)
    // {
    //     $this->setParentId($value ? $value->getKey() : 0)
    //         ->setRelation('parent', $value);

    //     return $this;
    // }
    
    // public function isRoot()
    // {
    //     return is_null($this->getParentId())||$this->getParentId()==0;
    // }
    
    //获取区域设备
    public function getAreaDevices($areaId=null) {
        try {
            $query=$this->query();
            if($areaId) {
                $node = $this->find($areaId);
                //var_dump($node->isLeaf());
                $isLeaf = $node->isLeaf();
                $method = $isLeaf?'whereAncestorOrSelf':'whereDescendantOrSelf';
                $query->{$method}($areaId);
            }
            $list = $query
                ->select(['AreaId', 'Fid', 'AreaName', 'area_text as title', 'area_icon as iconSvg', 'area_img', '_lft', '_rgt'])
                ->with(['devices'=>function($query){
                    $query->select(['AreaId', 'pdi_index', 'dpt_id', 'pdi_name', 'SubType']);
                    $query->leftJoin('t_Subdevicetype', function($join) {
                        $join->on('t_Subdevicetype.ts_Maintypeid', '=', 't_prideviceinfo.dpt_id')->on('t_Subdevicetype.ts_typeid', '=', 't_prideviceinfo.SubType');
                    })->addSelect(['ts_Icon']);
                }])
                ->get()
                ->toTree()
                ->toArray();
        } catch (\Throwable $th) {
           $list = [];
        }

        return $list;
    }
}
